﻿using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Me.Prettyprint.Cassandra.Utils
{
    /// <summary>
    /// MetricsRegistry is similar to yammer metrics, but stripped down.
    /// For Nectar we don't need all the features right now, so a minimal
    /// port was done to save time.
    /// </summary>
    public class MetricsRegistry
    {
        public MetricsRegistry()
        {
            Metrics = new ConcurrentDictionary<MetricName, Metric>();
            Timers = new ConcurrentDictionary<Metric, Timer>();
        }
        protected ConcurrentDictionary<MetricName, Metric> Metrics
        {
            get;
            private set;
        }
        public ConcurrentDictionary<Metric, Timer> Timers
        {
            get;
            private set;
        }
        public Timer NewTimer(MetricName mname, TimeUnit duration, TimeUnit rate)
        {
            Timer timer = null;
            Metric m = null;
            if (Metrics.ContainsKey(mname))
            {
                Metrics.TryGetValue(mname, out m);
                if (m != null)
                {
                    Timers.TryGetValue(m, out timer);
                    return timer;
                }
            }
            m = new Metric(mname);
            Metrics.TryAdd(mname, m);
            timer = new Timer(m);
            Timers.TryAdd(m, timer);
            return timer;
        }
    }
}
